using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Entities;
using MM.Business;

namespace MM
{
    public partial class frmHienthiPCK : Form
    {
        string _sConn = "";
        string _nguoitao = "";
        string _matau = "";
        List<CHI_TIET_PCK> lstCT = new List<CHI_TIET_PCK>();

        public frmHienthiPCK(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
            _nguoitao = frmDangNhap.objTaiKhoan._soCMND;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            this.HienthiPHIEU_CHUYEN_KHO_MAX(_matau);
            this.HienthiCHI_TIET_PCK();
            this.HienthiCHI_TIET_GT_ND(this.txtSoPhieuCK.Text.Trim());
        }

        private void HienthiPHIEU_CHUYEN_KHO_MAX(string matau)
        {
            clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);
            List<PHIEU_CHUYEN_KHO> lst = new List<PHIEU_CHUYEN_KHO>();
            lst = pxhCtrl.usp_SelectPHIEU_CHUYEN_KHO_MAX(matau);
            if (lst.Count > 0)
            {                
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                if (lst[0].NgayExport.HasValue)
                {
                    this.txtNgayDuyet.Text = ((DateTime)lst[0].NgayExport).Day.ToString() + "/" + ((DateTime)lst[0].NgayExport).Month.ToString() + "/" + ((DateTime)lst[0].NgayExport).Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                }
                this.txtSoPhieuCK.Text = lst[0].MaPhieuChuyenKho;              
            }
        }

        private void HienthiPHIEU_CHUYEN_KHO(string maphieuchuyenkho)
        {
            clsPHIEU_CHUYEN_KHO pxhCtrl = new clsPHIEU_CHUYEN_KHO(_sConn);
            List<PHIEU_CHUYEN_KHO> lst = new List<PHIEU_CHUYEN_KHO>();
            lst = pxhCtrl.usp_SelectPHIEU_CHUYEN_KHO(maphieuchuyenkho);
            if (lst.Count > 0)
            {
                this.txtNgayThucHien.Text = lst[0].NgayXuat.Day.ToString() + "/" + lst[0].NgayXuat.Month.ToString() + "/" + lst[0].NgayXuat.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiXuat;
                if (lst[0].NgayExport.HasValue)
                {
                    this.txtNgayDuyet.Text = ((DateTime)lst[0].NgayExport).Day.ToString() + "/" + ((DateTime)lst[0].NgayExport).Month.ToString() + "/" + ((DateTime)lst[0].NgayExport).Year.ToString();
                }
                else
                {
                    this.txtNgayDuyet.Text = "";
                }
            }
            else
            {
                this.txtNgayThucHien.Text = "";
                this.txtNguoiThucHien.Text = "";
                this.txtNgayDuyet.Text = "";
            }
        }

        private void HienthiCHI_TIET_GT_ND(string MaPhieuChuyenKho)
        {
            try
            {
                if (MaPhieuChuyenKho != "")
                {
                    clsCHI_TIET_ND_GT gtCtrl = new clsCHI_TIET_ND_GT(_sConn);
                    List<CHI_TIET_ND_GT> lstGT = new List<CHI_TIET_ND_GT>();
                    lstGT = gtCtrl.usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(MaPhieuChuyenKho);

                    this.gridControl2.DataSource = lstGT;
                }
            }
            catch { }

        }

        private void HienthiCHI_TIET_PCK()
        {
            clsCHI_TIET_PCK ctpxhCtrl = new clsCHI_TIET_PCK(_sConn);
            lstCT = ctpxhCtrl.usp_SelectCHI_TIET_PCK_ByMaPhieuChuyenKho(this.txtSoPhieuCK.Text.Trim());
            //lstCT.RemoveAll(delegate(CHI_TIET_PCK x) { return x.TinhTrang == 1; });
            this.gridControl1.DataSource = lstCT;
        }

        private void txtSoPhieuCK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtSoPhieuCK.Text.Trim().Length == 10)
                {
                    this.HienthiPHIEU_CHUYEN_KHO(this.txtSoPhieuCK.Text.Trim());
                    this.HienthiCHI_TIET_PCK();
                }
            }
        }

       

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuCK f = new frmTimPhieuCK(_sConn);
            f.chonPCK += new frmTimPhieuCK.ChonPhieuChuyenKho(GetPhieuChuyenKho);
            f.Show(this);
        }
        private void GetPhieuChuyenKho(object sender, ChonPhieuCKEventArgs e)
        {
            string mapck = e.MaPhieuChuyenKho;
            this.txtSoPhieuCK.Text = mapck;

            this.HienthiPHIEU_CHUYEN_KHO(mapck);
            this.HienthiCHI_TIET_PCK();
            this.HienthiCHI_TIET_GT_ND(mapck);
        }

        private void frmHienthiPCK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }
    }
}